{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GHZ State\n",
    "\n",
    "The GHZ state, a highly entangeld state entengaling all qubits in a circuit.\n",
    "\n",
    "$$\n",
    "|GHZ\\rangle = \\frac{|0\\rangle^{\\otimes n} + |1\\rangle^{\\otimes n}}{\\sqrt{2}}\n",
    "$$\n",
    "\n",
    "Create a function that will generate a GHZ state for `n` qubits. Use the Classiq build in `repeat` no classical loops. An example circuit is shown below. As you can see to create this circuit, there are two steps:\n",
    "\n",
    "1. Apply the H gate to the first qubit.\n",
    "2. Perform a CNOT gate between the first qubit and all other qubits, or perform CNOT gates like seen in the image below.\n",
    "\n",
    "<img src=\"\" />\n",
    "\n",
    "The Classiq library also has a QHZ state preparation build in. That implementation can be found [here](https://github.com/Classiq/classiq-library/blob/main/functions/open_library_definitions/prepare_ghz_state.qmod).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:26:33.289983Z",
     "iopub.status.busy": "2024-05-07T14:26:33.289381Z",
     "iopub.status.idle": "2024-05-07T14:26:37.833979Z",
     "shell.execute_reply": "2024-05-07T14:26:37.833226Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/109c864c-1ee0-4886-86df-1a66b8f1c3f7?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "from classiq import *\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(reg: Output[QArray]):\n",
    "    allocate(6, reg)\n",
    "    # your code here\n",
    "    pass\n",
    "\n",
    "\n",
    "qprog = synthesize(create_model(main))\n",
    "show(qprog)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The full solution for your reference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:26:37.963005Z",
     "iopub.status.busy": "2024-05-07T14:26:37.962179Z",
     "iopub.status.idle": "2024-05-07T14:26:40.240643Z",
     "shell.execute_reply": "2024-05-07T14:26:40.239952Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/ad6f8b4d-efcb-492e-8e29-624ff3f60a16?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "from classiq import *\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(reg: Output[QArray]):\n",
    "    allocate(6, reg)\n",
    "    H(reg[0])\n",
    "    repeat(\n",
    "        count=reg.len - 1,\n",
    "        iteration=lambda index: CX(control=reg[index], target=reg[index + 1]),\n",
    "    )\n",
    "\n",
    "\n",
    "qprog = synthesize(create_model(main))\n",
    "show(qprog)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
